



cap prog drop generate_iv_ols_supply
prog generate_iv_ols_supply
	syntax, instrument_var(str) xvars(str) yr(str) [fixedeffects(str) CLustvar(str) ]
	eststo clear
	foreach x in DomBK DomNB DomCB ForBK ForNB ForCB{

		eststo: reg l_`x'_mkt l_gdp `xvars' `fixedeffects' // Note: using logs gets rid of all zeros
		predict fitted_`x'
	}

		
	eststo clear
	cap drop sum_fitted
	gen sum_fitted = exp(fitted_DomBK)+exp(fitted_DomNB)+exp(fitted_DomCB)+exp(fitted_ForBK)+exp(fitted_ForNB)+exp(fitted_ForCB)
	drop fitted_DomBK fitted_DomNB fitted_ForBK fitted_ForNB fitted_DomCB fitted_ForCB
	
	cap drop fitted_total
	eststo: reg tot_gdp lag_tot_gdp l_gdp `xvars' `fixedeffects'
	predict fitted_total
	cap drop fitted_total2
	by country: gen fitted_total2 = fitted_total*gdp
 	
		
		
	cap drop price_fit_supply
	gen price_fit_supply = sum_fitted / fitted_total2
	
	cap drop `instrument_var'_price
	rename price_fit_supply `instrument_var'_price
	
	cap drop `instrument_var'_l_price
	gen `instrument_var'_l_price = ln(`instrument_var'_price)	
	
	cap drop `instrument_var'_yield
	gen `instrument_var'_yield = (1/`instrument_var'_price)^(1/`yr')-1
end

